package com.sanjiahantang.youjiarole.module.common.presenters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.base.BaseBean;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseMvpPresenter;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.common.beans.StringBean;
import com.sanjiahantang.youjiarole.module.common.views.ChangePasswordView;
import com.sanjiahantang.youjiarole.module.login.models.ILoginModel;
import com.sanjiahantang.youjiarole.module.login.models.impl.LoginModel;
import com.sanjiahantang.youjiarole.module.login.views.impl.LoginActivity;
import com.sanjiahantang.youjiarole.utils.SPUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zy on 2017/9/4.
 */

public class ChangePasswordPresenter extends BaseMvpPresenter<ChangePasswordView> implements CallBack<BaseBean> {

    private ILoginModel loginModel;
    public ChangePasswordPresenter(){
        loginModel = new LoginModel();
    }
    public void submit(Context context, String oldPass, String newPass) {
        Map<String,Object> params=new HashMap<>();
        params.put("token", ConfigValue.DATA_KEY);
        params.put("phone", SPUtils.get(context, SPConfig.PHONE,"").toString());
        params.put("pwd",oldPass);
        params.put("nowPwd",newPass);
        mView.showProgress();
        loginModel.doChangePassword(context,params,new ChangePasswordCallBack());
    }
    private class ChangePasswordCallBack implements CallBack<BaseBean>{
        @Override
        public void onSuccess(BaseBean response, Context context,Object tag) {
            mView.hideProgress();
            if(response.getCode() == RESPONSE_CODE_SUC){
                Utils.showToast(context, response.getMessage());
                LoginActivity.logout(context);
            }
        }
        @Override
        public void onFailure(String message) {
            mView.hideProgress();
        }
    }
    @Override
    public void onSuccess(BaseBean response, Context context,Object tag) {
        mView.hideProgress();
        if (response.getCode() == RESPONSE_CODE_SUC)
        {
            Intent intent=new Intent(context,LoginActivity.class);
            context.startActivity(intent);
            ((BaseMvpActivity)context).finish();
        }
    }

    @Override
    public void onFailure(String message) {
        mView.hideProgress();
    }

    public void modifyPayPwd(final Context context, String oldPass, String newPass, BaseView.OnLoadListener listener) {
        Map<String,Object> params=new HashMap<>();
        params.put("token", ConfigValue.DATA_KEY);
        params.put("phone", SPUtils.get(context, SPConfig.PHONE,"").toString());
        params.put("pwd",oldPass);
        params.put("nowPwd",newPass);
        if (mView == null) return;
        mView.showProgress();
        loginModel.modifyPayPwd(context,params, new CallBack<StringBean>() {
            @Override
            public void onSuccess(StringBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response == null || response.getCode() != RESPONSE_CODE_SUC){
                    Utils.showToast(context,"修改失败");
                    return;
                }
                if (TextUtils.equals(response.getData(),"true")){
                    Utils.showToast(context, response.getMessage());
                    ((Activity)context).finish();
                }else{
                    Utils.showToast(context,"修改失败");
                }

            }


            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
                Utils.showToast(context, "修改失败");
            }
        });
    }
}
